<?php
namespace app\system\controller;

use app\common\model\AreaModel;
use app\common\model\AuthRuleModel;
use app\common\model\CmsCategoryModel;
use app\common\model\logic\CarOrderModel;
use app\common\model\MoudleModel;
use app\common\model\ConfigModel;
use app\common\model\DictModel;
use app\common\model\UserModel;
use app\common\model\UserWxModel;
use think\Cookie;
use think\Db;
use think\Session;

class IndexController extends BaseController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     * @return mixed
     */
    public function index()
    {
        $userModel = new UserModel();
        if(is_mobile()==3 && Cookie::has('wxuser')){ //微信端补充用户资料
            $wxuser = Cookie::get('wxuser');
            $userWxModel = new UserWxModel();
            $userWxModel->bindUser($this->getUid(),$wxuser);
            $wxuser = $userWxModel->where('uid',$this->getUid())->find();
            $this->assign('wxuser',$wxuser);
        }
        $userinfo  = $userModel->get($this->getUid());
        $title     = '控制台';
        $ruleList  = $userModel->getMyRuleList($this->getGroupId(), $this->getUtype());
        $isDrive = $this->isDriver();
        $this->assign('title', $title);
        $this->assign('userinfo', $userinfo);
        $this->assign('group_id', $this->getGroupId());
        $this->assign('ruleList', $ruleList);
        if($isDrive>0 && is_mobile()!=2){
            //获取本月数据
            $carOrderModel = new CarOrderModel();
            $static = $carOrderModel
                ->field("sum(order_mileage) as total_order_mileage,count(*) as total_count,sum(order_l) as total_order_l")
                ->whereTime('order_start_time','month')
                ->where("find_in_set('{$isDrive}',fleet_driver_ids)")
                ->find();
            //echo $carOrderModel->getLastSql();
            $this->assign('static',$static);
            return $this->fetch('driver'); //司机界面
        }
        if($this->getGroupId()!=104 || $this->getUid()==1){
            return $this->fetch();
        }
        $this->error('您好，您没有登录权限，请切换账号重新登录！','Public/login');
    }

    /**
     * 右侧菜单
     */
    public function getMenu(){
        $userModel = new UserModel();
        $groupid = $this->request->param('groupid');
        $ruleid = $this->request->param('ruleid');
        $ruleList  = $userModel->getMyRuleList($groupid, $this->getUtype());
        $this->assign('ruleList',$ruleList[$ruleid]['children']);
        echo $this->fetch();
    }

    /**
     * 主窗口
     */
    public function main()
    {
        return $this->fetch();
    }

    /**
     * 初始化数据
     */
    public function initCache()
    {
        $authRuleModel      = new AuthRuleModel();
        $authRuleModel->updateCache($this->getUtype()); //更新权限规则缓存
        $arr[] = "规则缓存[auth_rule_{$this->getUtype()}]更新完毕！";
        $this->assign('arr', $arr);
        return $this->fetch();
    }

    /**
     * 退出登录
     */
    public function exitLogin()
    {
        Cookie::delete($this->getUtype().'Des');
        Session::delete('group_id');
        Cookie::delete('wxuser');
        if(is_mobile()==3){ //解除对应微信绑定
            $userWxModel = new UserWxModel();
            $userWxModel->where('uid',$this->getUid())->delete();
        }
        $this->success('退出登录成功！', url('Public/login'));
    }

}
